const db = require('../mysql/db').promise()

class OrdersModel {
    // 根据id获取用户信息
    static async getOrders(id) {
        console.log('123')
        try {
            // 如果有id参数，则根据id查询用户信息
            if (id) {
                const [rows] = await db.query('select * from orders where id=?', [id])
                return rows
            }
            // 如果没有id参数，则查询所有用户信息
            let [rows] = await db.query('select * from orders')
            return rows
        } catch (err) {
            // 捕获错误并打印
            console.log('error==', err);

        }
    }

    static async addOrders({ order_number, username, order_time, amount,status,created_at }) {
        try {
            let [result] = await db.query('insert into orders (order_number,username,order_time,amount,status,created_at) values (?,?,?,?,?,?)',
                [order_number, username, order_time, amount,status,created_at])
        }catch(err){
            console.log('error==', err);
        }
    }

    static async updateOrders(id, data) {
        try {
            const [result] = await db.query('update orders set ? where id=?', [data, id])
            return result
        }catch(err){
            console.log('error==', err);
        }
    }

    static async deleteOrders(id) {
        try {
            // 执行删除操作
            const [result] = await db.query('delete from orders where id=?', [id])
            return result
        } catch (err) {
            // 捕获错误并打印
            console.log('error==', err);
        }
    }

}


module.exports = OrdersModel